package cn.tedu.charging.cost.dao.repository.impl;

import cn.tedu.charging.cost.dao.repository.CostRuleCacheRepository;
import cn.tedu.charging.cost.pojo.po.CostRulePO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CostRuleCacheRepositoryImpl implements CostRuleCacheRepository {

    @Autowired
    private RedisTemplate<String,List<CostRulePO>> redisTemplate;

    private static final String COST_RULE_KEY = "COST_RULE_";

    @Override
    public List<CostRulePO> getCostRuleByStationId(Integer stationId) {
        //opsForValue 是用来操作 Redis的String 类型
        ValueOperations<String,List<CostRulePO>>
                valueOperations = redisTemplate.opsForValue();
        return valueOperations.get(COST_RULE_KEY + stationId);
    }

    @Override
    public void saveCostRules(Integer stationId, List<CostRulePO> costRules) {
        //opsForValue 是用来操作 Redis的String 类型
        ValueOperations<String,List<CostRulePO>>
                valueOperations = redisTemplate.opsForValue();
        valueOperations.set(COST_RULE_KEY + stationId,costRules);
    }
}
